﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
//using UnityEditor;

public class ResourceManager
{
    public static readonly ResourceManager instance = new ResourceManager();
    public static AssetBundleManifest m_Manifest;
    public Dictionary<string, AssetInfo> assetDic = new Dictionary<string, AssetInfo>();
    private string platformPath = null;
    private string m_Path = null;
    public void PreLoad()
    {
        platformPath = PathContrl.GetApplicationPlatform();
        m_Path = Path.Combine(Application.persistentDataPath, "Resources/" + platformPath + "/");
        LoadManifest();
    }
    public void LoadAsst(string path, Action<AssetInfo> act = null)
    {
        string target = m_Path + path;
        LoadAsset(target, act);
    }
    private string HandlePath(string path)
    {
        string[] tmp = path.Split('/');
        string resoult = tmp[tmp.Length - 1];
        tmp = null;
        return resoult;
    }
    public Action<AssetInfo> LoadCompleteCallBack = delegate (AssetInfo info)
      {
          info.Load();
          GameObject obj = GameObject.Instantiate(info.obj) as GameObject;
          info.obj = null;
          info.Asset.Unload(false);

      };
    public void LoadManifest()
    {
        string manifestPath = m_Path + platformPath + ".dat";     // Path.Combine(Application.persistentDataPath, "Resources/IOS/IOS.dat");
        byte[] bytes = File.ReadAllBytes(manifestPath);
        AssetBundle bundle = AssetBundle.LoadFromMemory(bytes);
        m_Manifest = (AssetBundleManifest)bundle.LoadAsset("AssetBundleManifest");
        bundle.Unload(false);
        bytes = null;
        Debug.Log(m_Manifest.name);
    }
    public void LoadAsset(string path, Action<AssetInfo> SucceedAction = null, Action<UnityEngine.Object> Failed = null)
    {
        AssetInfo info;
        if (assetDic.ContainsKey(path))
        {
            info = assetDic[path];
        }
        byte[] bytes = File.ReadAllBytes(path);
        AssetBundle bundle = AssetBundle.LoadFromMemory(bytes);
        string[] dependences = m_Manifest.GetAllDependencies(bundle.name);
        info = new AssetInfo();
        info.Dependences = new AssetBundle[dependences.Length];
        for (int i = 0; i < info.Dependences.Length; i++)
        {
            info.Dependences[i] = AssetBundle.LoadFromFile(Application.persistentDataPath + "/Resources/" + platformPath + "IOS/" + dependences[i]);

        }
        info.Asset = bundle;
        string name = HandleAssetBundleName(bundle.name);
        info.Name = name;
        if (SucceedAction != null)
        {
            SucceedAction(info);
        }

    }
    private string HandleAssetBundleName(string name)
    {
        string[] tmp = name.Split('/');
        string tmpName = tmp[tmp.Length - 1];
        string resoult = tmpName.Replace(".dat", "");
        tmp = null;
        tmpName = null;
        return resoult;
    }
}
public class AssetInfo
{
    public AssetInfo()
    {

    }
    public AssetInfo(string name, AssetBundle bundle)
    {
        Name = name;
        Asset = bundle;
    }
    public string Name
    {
        get;
        set;
    }
    public AssetBundle Asset
    {
        get;
        set;
    }
    public bool KeepinMemory
    {

        get;
        set;
    }
    public UnityEngine.Object obj
    {
        get;
        set;
    }
    public AssetBundle[] Dependences;

    public void Load()
    {
        AssetBundleRequest re = Asset.LoadAssetAsync(Name);
        obj = re.asset;
        re = null;
    }
    public void Release()
    {
        Asset.Unload(false);
        if (Dependences.Length > 0)
        {
            for (int i = 0; i < Dependences.Length; i++)
            {
                Dependences[i].Unload(false);
            }
            Dependences = null;
        }
    }
}
public class PathContrl
{
    public static string GetApplicationPlatform()
    {
        switch (Application.platform)
        {
            case RuntimePlatform.Android:
                return "Android";

            case RuntimePlatform.IPhonePlayer:
                return "IOS";

            case RuntimePlatform.OSXEditor:
                return "OSX";
            default:
                return null;

        }
    }

}
